/*
 * Created by JFormDesigner on Mon Jun 24 15:05:19 CST 2024
 */

package scct.zhangyang.ui;

import scct.zhangyang.dao.UserDao;
import scct.zhangyang.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 13239
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) {
//        String name = txtName.getText();
//        String pass = txtPass.getText();
//        System.out.println(name+","+pass);
//        if(!name.matches("^\\w{3,10}$")){
//            JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请检查后重新输入");
//            return;
//        }
//        if(!pass.matches("^\\w{3,10}$")){
//            JOptionPane.showMessageDialog(null,"你输入的密码不正确，请检查后输入");
//            return;
//        }
//        UserDao userDao=new UserDao();
//        User user=userDao.login(name,pass);
//        if(user==null){
//            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后重新输入");
//            return;
//        }else {
//            JOptionPane.showMessageDialog(this,"登陆成功");
//            return;
//        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here
        String name = txtName.getText();
        String pass = txtPass.getText();
        System.out.println(name + "," + pass);
        if (!name.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(null, "你输入的用户名不合法，请检查后重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(null, "你输入的密码不正确，请检查后输入");
            return;
        }
        UserDao userDao = new UserDao();
        User user = userDao.login(name, pass);
        if (user == null) {
            JOptionPane.showMessageDialog(this, "用户名或密码错误，请检查后重新输入");

        } else {
            JOptionPane.showMessageDialog(this, "登陆成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(1200,800);
            formMain.setLocationRelativeTo(null);
            formMain.setVisible(true);
            this.dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        label4 = new JLabel();
        txtName = new JTextField();
        txtPass = new JPasswordField();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 10f));
        contentPane.add(label1);
        label1.setBounds(170, 50, 55, 40);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(30, 120), label2.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u5bc6\u7801");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(40, 170), label4.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(105, 115, 190, 30);
        contentPane.add(txtPass);
        txtPass.setBounds(105, 165, 190, 30);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> button1(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(105, 230), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(220, 230), btnReg.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(365, 340);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JLabel label4;
    private JTextField txtName;
    private JPasswordField txtPass;
    private JButton btnLogin;
    private JButton btnReg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
